// 处理文章相关的IPC通信
const { dbOperations } = require('./database');

function setupArticleIpcHandlers(db, ipcMain) {
  // 获取所有文章
  ipcMain.handle('get-articles', async (event, options) => {
    return dbOperations.getArticles(db, options);
  });
  
  // 获取单篇文章详情
  ipcMain.handle('get-article-by-id', async (event, articleId) => {
    return dbOperations.getArticleById(db, articleId);
  });
  
  // 获取分类统计
  ipcMain.handle('get-category-stats', async () => {
    return dbOperations.getCategoryStats(db);
  });
  
  // 获取热门文章
  ipcMain.handle('get-popular-articles', async (event, limit) => {
    return dbOperations.getPopularArticles(db, limit);
  });

  // 新增文章
  ipcMain.handle('add-article', async (event, articleData) => {
    return dbOperations.addArticle(db, articleData);
  });

  // 更新文章
  ipcMain.handle('update-article', async (event, { articleId, articleData }) => {
    return dbOperations.updateArticle(db, { articleId, articleData });
  });

  // 删除文章
  ipcMain.handle('delete-article', async (event, articleId) => {
    return dbOperations.deleteArticle(db, articleId);
  });

    
  // 日志相关IPC处理
  ipcMain.handle('get-timeline-entries', async () => {
    return dbOperations.getTimelineEntries(db);
  });
  
  ipcMain.handle('get-timeline-entry-by-id', async (event, id) => {
    return dbOperations.getTimelineEntryById(db, id);
  });
  
  ipcMain.handle('add-timeline-entry', async (event, entryData) => {
    return dbOperations.addTimelineEntry(db, entryData);
  });
  
  ipcMain.handle('update-timeline-entry', async (event, { id, entryData }) => {
    return dbOperations.updateTimelineEntry(db, { id, entryData });
  });
  
  ipcMain.handle('delete-timeline-entry', async (event, id) => {
    return dbOperations.deleteTimelineEntry(db, id);
  });
}

module.exports = { setupArticleIpcHandlers };